package org.jeecg.modules.third.openapi;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.third.openapi.utils.httpsCall;
import org.jeecg.modules.wlhy.hyconfig.entity.HyConfig;
import org.jeecg.modules.wlhy.hyvehicle.entity.HyVehicle;
import org.jeecg.modules.wlhy.hyvehicle.mapper.HyVehicleMapper;
import org.jeecg.modules.wlhy.utils.HyConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class XDOpenApiService {



    @Value(value = "${xd.baseUrl:}")
    private String baseUrl;
    @Autowired
    private HyVehicleMapper vehicleMapper;



    //获取轨迹
    public  String getRouterPath(String vehicleNo, String vco, String startTime, String endTime){

        try {
            String url = baseUrl +"/api/dataApi/V1.0.2/save/apis/routerPath";
            HyVehicle vehicle = vehicleMapper.getHyVehicleByVehicleNumber(vehicleNo);
            HyConfig systemConfig = HyConfigUtils.getConfig();
            Map<String, Object> reqMap = new HashMap<>();
            reqMap.put("cid", systemConfig.getXdCid());
            reqMap.put("srt", systemConfig.getXdSrt());
           /* reqMap.put("carPlateNo", "冀A488B1");
            reqMap.put("carPlateColor","2");
            reqMap.put("vin","LZFH25W4XKD025033");*/
            reqMap.put("carPlateNo",vehicleNo);
            reqMap.put("carPlateColor",vco);
            reqMap.put("vin",vehicle.getVin());
            reqMap.put("beginTime",startTime);
            reqMap.put("endTime",endTime);

            log.info("信达请求历史轨迹参数 -> {} {}", url, reqMap);

            String res = httpsCall.httpsCall(url,reqMap,false);

                          log.info("信达返回参数回执 <- {}" + res);

            JSONObject jsonObject = JSONObject.parseObject(res);
            String status = jsonObject.getString("status");

            if (status.equals("1001")){
                return res;
            }else {
                throw new JeecgBootException("信达轨迹获取失败: " + jsonObject.getString("msg"));
            }

        } catch (Exception e) {
            log.info("信达车辆轨迹数据获取异常 {}", e.getMessage());
            e.printStackTrace();
            throw new JeecgBootException(e.getMessage());
        }
    }




}
